import request from "@/utils/request";

const BASE_URL = "/api/v1/product/";

const ProductAPI = {
  getInfo(id: string) {
    return request<any, ProductPageVO>({
      url: `${BASE_URL}${id}/`,
      method: "get",
    });
  },
  getPage(queryParams: ProductPageQuery) {
    return request<any, PageResult<ProductPageVO[]>>({
      url: `${BASE_URL}`,
      method: "get",
      params: queryParams,
    });
  },

  getFormData(id: string) {
    return request<any, ProductForm>({
      url: `${BASE_URL}${id}/`,
      method: "get",
    });
  },
  add(data: ProductForm) {
    return request({
      url: `${BASE_URL}`,
      method: "post",
      data: data,
    });
  },
  update(id: string, data: ProductForm) {
    return request({
      url: `${BASE_URL}${id}/`,
      method: "patch",
      data: data,
    });
  },
  getOptions(query: string) {
    return request<any, OptionType[]>({
      url: `${BASE_URL}options/`,
      method: "get",
      params: {
        search: query,
      },
    });
  },
};
export default ProductAPI;

interface ProductPageQuery extends PageQuery {
  /** 搜索关键字 */
  search?: string;
  bom_material_set__product__id?: string;
}

interface ProductForm {
  id?: string;
  name?: string;
  title?: string;
  company?: string;
  weight?: number;
  price?: number;
  is_update?: boolean;
  status?: number;
  unit?: number;
  customer_product_set?: Customer[];
}
interface BomForm {
  product?: string;
  material?: string;
  quantity?: number;
}

interface ProductPageVO {
  id: string;
  name: string;
  title?: string;
  company: string;
  parent?: string;
  weight: number;
  price?: number;
  dwgs?: string[];
  status: number;
  create_time?: Date;
  update_time?: Date;
  create_user?: string;
  update_user?: string;
  has_child?: boolean;
  unit_name?: string;
  barcode: string;
}

interface Customer {
  id?: number;
  product?: string;
  company?: string;
  dwg?: string;
}

export { ProductPageVO, ProductPageQuery, ProductForm, BomForm };
